package com.ppd.yygh.order.controller;

import com.ppd.yygh.common.result.Result;
import com.ppd.yygh.order.service.PaymentService;
import com.ppd.yygh.order.service.WeixinService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@Api("微信支付接口")
@RestController
@RequestMapping("auth/order/weixin")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;
    @Autowired
    private PaymentService paymentService;

    //生成微信二维码
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId ){
       Map map =  weixinService.createNative(orderId);
       return Result.ok(map);
    }

    //查询支付状态
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId){
        //调用微信接口实现支付状态的查询
        Map<String,String> resultMap =  weixinService.queryPayStatus(orderId);
        //判断
        if (resultMap == null){
            return Result.fail().message("支付错误");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))){
            //跟新订单状态
            String out_trade_no = resultMap.get("out_trade_no");
            paymentService.paySuccess(out_trade_no,resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }

}
